package com.xtxk.contorller;

import com.xtxk.entity.ProductEntity;
import com.xtxk.service.user.IOrderService;
import com.xtxk.service.user.IProductService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xtxk.utils.result.Result;

@RestController
@RequestMapping(value = "/test")
public class TestController {

    static int n = 1;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IProductService iProductService;

    /**
     * 压力测试请求
     */
    @GetMapping(value = "/request")
    public Result<?> request(@RequestParam("pId") int pId) {
        System.out.println("jMeter request begin " + pId);
        // 1 获取当前商品库存是否还有
        ProductEntity productEntity = iProductService.getGoodNumber(pId);
        if (productEntity.getGoodsNumber() <= 0) {
            return Result.ok("商品已秒杀完");
        }

        try {
            // 存在库存，减库存 创建的订单
            iOrderService.secKill(productEntity);
        } catch (Exception ex) {
            ex.printStackTrace();


        }
        return Result.ok();
    }
}
